package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetTopicFavoriteBean;
import com.rjwh.dingdong.client.bean.localbean.TopicFavoriteData;
import com.rjwh.dingdong.client.bean.localbean.TopicFavoriteListData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.MoreCollectionModel;
import com.rjwh.dingdong.client.mvp.presenter.IMoreCollectionPresenter;
import com.rjwh.dingdong.client.mvp.view.MoreCollectionView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author Kay
 * @ClassName MoreCollectionPresenter
 * @Description
 * @date 2016-8-5
 * @history 2016-8-5 author: description:
 */
public class MoreCollectionPresenter extends BasePresenter<MoreCollectionView> implements IMoreCollectionPresenter {

    private MoreCollectionModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public MoreCollectionPresenter(Context context, MoreCollectionView mMvpView) {
        super(context, mMvpView);
        model = new MoreCollectionModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            switch (businessType) {
                case NetConstant.GETTOPICFAVORITE:
                    getMvpView().setRefreshing(false);
                    ResGetTopicFavoriteBean resGetTopicFavoriteBean = (ResGetTopicFavoriteBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetTopicFavoriteBean != null) {
                        TopicFavoriteData topicFavoriteData = resGetTopicFavoriteBean.getData();
                        if (topicFavoriteData != null) {
                            List<TopicFavoriteListData> topiclist = topicFavoriteData.getTopiclist();
                            if (topiclist != null) {
                                if (topiclist.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_collection));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                getMvpView().loadData(isRefresh, topiclist);
                            }
                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

    /**
     * 获取收藏列表
     *
     * @param pageindex 当前页码
     * @param isRefresh 是否刷新
     * @param direction
     */
    @Override
    public void getTopicFavorite(int pageindex, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.getTopicFavorite(pageindex, isRefresh);
    }
}
